<template>
    <div>
        <!--    搜索表单-->
        <div style="margin-bottom: 20px">
            <el-input style="width: 240px" placeholder="请输入图书名称" v-model="params.bookName"></el-input>
            <el-input style="width: 240px; margin-left: 5px" placeholder="请输入图书的标准码" v-model="params.bookNo"></el-input>
            <el-input style="width: 240px; margin-left: 5px" placeholder="请输入用户名称" v-model="params.userName"></el-input>
            <el-button style="margin-left: 5px" type="primary"  @click="load"><i class="el-icon-search"></i> 搜索
            </el-button>
            <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh"></i> 重置
            </el-button>
        </div>

        <el-table :data="tableData" stripe row-key="id" default-expand-all>
            <el-table-column prop="id" label="编号" width="80px"></el-table-column>
            <el-table-column prop="bookName" label="图书名称"></el-table-column>
            <el-table-column prop="bookNo"  label="图书标准码"></el-table-column>
            <el-table-column prop="userNo" label="用户会员号"></el-table-column>
            <el-table-column prop="userName" label="用户名称"></el-table-column>
            <el-table-column prop="userPhone" label="用户联系方式"></el-table-column>
            <el-table-column prop="score" label="借书金额"></el-table-column>
            <el-table-column prop="status" label="借书状态"></el-table-column>
            <el-table-column prop="days" label="借书天数"></el-table-column>
            <el-table-column prop="createTime" label="借出时间"></el-table-column>
            <el-table-column prop="returnDate" label="归还日期"></el-table-column>
            <el-table-column prop="note" label="过期提醒">
                <template v-slot="scope">
                    <el-tag type="success" v-if="scope.row.note==='正常'">{{scope.row.note}}</el-tag>
                    <el-tag type="primary" v-if="scope.row.note==='即将到期'">{{scope.row.note}}</el-tag>
                    <el-tag type="warning" v-if="scope.row.note==='已到期'">{{scope.row.note}}</el-tag>
                    <el-tag type="danger" v-if="scope.row.note==='已过期'">{{scope.row.note}}</el-tag>

                </template>
            </el-table-column>
            <el-table-column  label="管理">
                <template v-slot="scope">
<!--                    <el-button type="primary" @click="returnBooks(scop.row) " v-if="scop.row.status==='已借出'">还书</el-button>-->

                        <el-popconfirm
                                style="margin-left: 5px"
                                title="您确定要还这本书吗？"
                                @confirm="returnBooks(scope.row)"
                        >
                            <el-button type="primary"  slot="reference" v-if="scope.row.status==='已借出'">还书</el-button>
                        </el-popconfirm>


                </template>
            </el-table-column>
<!--            <el-table-column prop="updateTime" label="修改时间"></el-table-column>-->


            <el-table-column label="操作" >
                <template v-slot="scope">
                    <el-popconfirm
                            style="margin-left: 5px"
                            title="您确定要删除这行数据吗？"
                            @confirm="del(scope.row.id)"
                    >
                        <el-button type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>

                </template>
            </el-table-column>

        </el-table>


        <!--    分页-->
        <div style="margin-top: 20px">
            <el-pagination
                    background
                    :current-page="params.pageNum"
                    :page-size="params.pageSize"
                    layout="prev, pager, next"
                    @current-change="handleCurrentChange"
                    :total="total">
            </el-pagination>
        </div>


    </div>
</template>

<script>
    import request from "@/utils/request";
    import Cookies from "js-cookie"

    export default {
        name: 'Borrow',
        data() {
            return {
                tableData: [],
                total: 0,
                admin: Cookies.get('admin') ? JSON.parse(Cookies.get('admin')) : {},
                params: {
                    pageNum: 1,
                    pageSize: 10,
                    bookName: '',
                    bookNo: '',
                    userName:''


                },
            }
        },
        created() {
            this.load()

        },
        methods: {
            //搜索
            load() {
                // fetch('http://localhost:8000/user/list').then(res =>res.json()).then(res => {
                //     console.log(res)
                //     this.tableData=res
                // })
                // console.log(1)
                request.get('/borrow/page', {
                    params: this.params
                }).then(res => {
                    if (res.code === '200') {
                        this.tableData = res.data.list
                        this.total = res.data.total
                    }
                })
            },
            //重置并加载
            reset() {
                this.params = {
                    pageNum: 1,
                    pageSize: 10,
                    bookName: '',
                    bookNo: '',
                    userName: ''
                }
                this.load()

            },
            //点击分页按钮出发分页
            handleCurrentChange(pageNum) {
                this.params.pageNum = pageNum
                this.load()//重新加载


            },
            //删除
            del(id) {
                request.delete("/borrow/delete/" + id).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('删除成功')
                        //删除成功后加载数据
                        this.load()
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            //还书
            returnBooks(row){
                request.post("/return/addReturn",row ).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('还书成功')
                        this.$router.push("/ReturnBooksList")
                        //this.load()
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            }

        }
    }

</script>

<style scoped>

</style>